package switchviz.gui;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JPanel;

import org.jdom.JDOMException;

import switchviz.Application;
import switchviz.core.ResourceManager;
import switchviz.core.view.ViewDataModel;
import switchviz.gui.component.JXMLComboBox;
import switchviz.recording.RecordDataModel;
import switchviz.util.FileOperations;

public class ExportDialog extends JDialog implements ActionListener
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 5575870694174142475L;
	public static final int EXPORTVIEW = 0;
	public static final int EXPORTRECORD = 1;
	private static final String ACTIONVIEW = "actionview";
	private static final String ACTIONREC = "actionrec";
	private JXMLComboBox _cmbview = null;
	private JXMLComboBox _cmbrec = null;
	private JButton _buttexport = null;
	private JButton _buttcancel = null;
	private JPanel _content = null;
	
	public ExportDialog(Frame owner, int type)
	{
		super(owner, true);
		try
		{
			switch(type)
			{
			case EXPORTVIEW:
				initializeView();
				break;
			case EXPORTRECORD:
				initializeExport();
				break;
			}
			initialize();
			this.pack();
			//this.setSize(400, 400);
			this.setLocationRelativeTo(null);
			this.setVisible(true);
			
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	private void initialize()
	{
		_buttexport.addActionListener(this);
		
		JPanel buttonpanel = new JPanel();
        buttonpanel.setLayout(new BoxLayout(buttonpanel, BoxLayout.LINE_AXIS));
        buttonpanel.add(Box.createHorizontalGlue());
       
		_buttcancel = new JButton("Cancel");
		_buttcancel.addActionListener(this);
		
		buttonpanel.add(_buttexport);
		buttonpanel.add(_buttcancel);
		
		this.setLayout(ResourceManager.getInstance().getBorderLayout());
		this.add(_content, BorderLayout.CENTER);
		this.add(buttonpanel, BorderLayout.SOUTH);
	}
	
	private void initializeView() throws JDOMException, IOException
	{
		this.setTitle("Export View");
		_content = new JPanel(ResourceManager.getInstance().getBorderLayout());
		
		_cmbview = new JXMLComboBox("Select View: ", JXMLComboBox.VIEWLIST);
		_buttexport = new JButton("Export");
		_buttexport.setActionCommand(ACTIONVIEW);
		
		_content.add(_cmbview, BorderLayout.CENTER);
	}
	
	private void initializeExport() throws JDOMException, IOException
	{
		this.setTitle("Export Record");
		_content = new JPanel(ResourceManager.getInstance().getBorderLayout());
		
		_cmbrec = new JXMLComboBox("Select Record: ", JXMLComboBox.RECORDLIST);
		_buttexport = new JButton("Export");
		_buttexport.setActionCommand(ACTIONREC);
		
		_content.add(_cmbrec, BorderLayout.CENTER);
	}

	public void actionPerformed(ActionEvent evt) 
	{
		String command = evt.getActionCommand();
		
		try
		{
			if(command.equals(ACTIONVIEW))
			{
				if(_cmbview.getComboBox().getSelectedIndex() < 0) return;
				
				ViewDataModel vdm = (ViewDataModel)_cmbview.getComboBox().getSelectedItem();
				
				final JFileChooser fc = new JFileChooser();
				fc.setFileFilter(FileOperations.getXMLFileFilter("View Configuration File"));
				int retval = fc.showSaveDialog(Application.getInstance().getMainFrame());
				if(retval == JFileChooser.APPROVE_OPTION)
				{
					File file = fc.getSelectedFile();
					FileOperations.copyFile(vdm.getFile(), file);
				}
			}
			else if(command.equals(ACTIONREC))
			{
				if(_cmbrec.getComboBox().getSelectedIndex() < 0) return;
				
				RecordDataModel rdm = (RecordDataModel)_cmbrec.getComboBox().getSelectedItem();
				
				final JFileChooser fc = new JFileChooser();
				fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				int retval = fc.showSaveDialog(Application.getInstance().getMainFrame());
				if(retval == JFileChooser.APPROVE_OPTION)
				{
					File file = fc.getSelectedFile();
					FileOperations.copyDirectory(rdm.getFolder(), new File(file, rdm.toString()));
				}
			}
			this.setVisible(false);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}

}
